package com.madi.company.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.madi.company.R;
import com.madi.company.app.GlobalApplication;
import com.madi.company.bean.JobAddressEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * ---获取城市list
 * ---根据服务器返回的字符获取对应的语言
 * ---分享功能
 * Created by 丁宁
 * on 2017/2/7.
 *
 */

public  class DnUtil {

    private static List<JobAddressEntity> list;

    // 获取初始化数据的中英文文件名称
    public static String getFileName() {
        String fileName;
        String sta = GlobalApplication.getAppContext().getResources().getConfiguration().locale.getLanguage();
        if ("zh".equals(sta)) {
            fileName = "zh_version_data.json";
        } else {
            fileName = "en_version_data.json";
        }
        return fileName;
    }

    // 获取城市list
    public static List<JobAddressEntity> getAddressList(){
        StringBuilder stringBuilder = new StringBuilder();

        try {
            AssetManager assetManager = GlobalApplication.getAppContext().getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(getFileName())));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonData = stringBuilder.toString();
        try {
            Gson gson = new Gson();
            JSONObject data = new JSONObject(jsonData);
            list = new ArrayList<JobAddressEntity>();
            List<JobAddressEntity> jobAddressBeans = gson.fromJson(data.optJSONArray("chinaCityList").toString(), new TypeToken<List<JobAddressEntity>>() {
            }.getType());
            List<JobAddressEntity> countryLists = gson.fromJson(data.optJSONArray("countryList").toString(), new TypeToken<List<JobAddressEntity>>() {
            }.getType());
            list.addAll(jobAddressBeans);
            list.addAll(jobAddressBeans.size(), countryLists);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 根据服务器返回的字符获取对应的语言
    public static String getLanguage(String languageStr){
        String name = "";
        String[] codes = new String[]{"zh", "en", "de", "fr", "ja", "ko",
                "yue", "pt", "ar", "es", "ru", "hi", "vi", "th", "fa", "ms", "jv", "tr",
                "it", "bn", "mr", "ta", "ur"};
       String[] strs = new String[]{GlobalApplication.getAppContext().getString(R.string.search_title_Chinese)
                , GlobalApplication.getAppContext().getString(R.string.search_title_English), GlobalApplication.getAppContext().getString(R.string.search_title_German)
                , GlobalApplication.getAppContext().getString(R.string.search_title_French), GlobalApplication.getAppContext().getString(R.string.search_title_Japanese)
                , GlobalApplication.getAppContext().getString(R.string.search_title_kor), GlobalApplication.getAppContext().getString(R.string.search_title_Cantonese)
                , GlobalApplication.getAppContext().getString(R.string.search_title_Portuguese), GlobalApplication.getAppContext().getString(R.string.search_title_Arabic)
                , GlobalApplication.getAppContext().getString(R.string.search_title_Spaish), GlobalApplication.getAppContext().getString(R.string.search_title_Russian)
                , GlobalApplication.getAppContext().getString(R.string.search_title_Dard), GlobalApplication.getAppContext().getString(R.string.search_title_yunan)
                , GlobalApplication.getAppContext().getString(R.string.search_title_taiguo), GlobalApplication.getAppContext().getString(R.string.search_title_bosi)
                , GlobalApplication.getAppContext().getString(R.string.search_title_malai), GlobalApplication.getAppContext().getString(R.string.search_title_guawa)
                , GlobalApplication.getAppContext().getString(R.string.search_title_tuerqi), GlobalApplication.getAppContext().getString(R.string.search_title_edali)
                , GlobalApplication.getAppContext().getString(R.string.search_title_mengjiala), GlobalApplication.getAppContext().getString(R.string.search_title_maladi)
                , GlobalApplication.getAppContext().getString(R.string.search_title_damier), GlobalApplication.getAppContext().getString(R.string.search_title_wuerqi)};

        for (int i = 0; i < codes.length; i++) {
            String code = codes[i];
            if (code.equals(languageStr)) {
                name = strs[i];
                break;
            }
        }
        return name;
    }

    /**
     *  分享功能
     * @param context 上下文
     */
    public static void showShare(Context context) {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        oks.setTitle(context.getString(R.string.share_app_names));
        oks.setTitleUrl("http://www.madisoft.cn/hrshare.html"); // 标题的超链接
        oks.setText(context.getString(R.string.share_app_fname));
        oks.setImageUrl("http://ww3.sinaimg.cn/large/006rY08ajw1f563hh95sxj303c03c0sq.jpg");
        oks.setUrl("http://www.madisoft.cn/hrshare.html");
        oks.setComment("-" + context.getString(R.string.app_name) + "-");
        oks.setSite(context.getString(R.string.app_name));
        oks.setSiteUrl("http://www.madisoft.cn/hrshare.html");
//        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
//            @Override
//            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
//                if (platform.getName().equalsIgnoreCase(SinaWeibo.NAME)) {
//                    paramsToShare.setText(getString(R.string.share_app_names) + "http://www.madisoft.cn/hrshare.html");
//                    paramsToShare.setImageUrl("http://www.madisoft.cn/image/extra/4.png");
//                    paramsToShare.setUrl(null);
//                }
//            }
//        });
        oks.show(context);
    }
}
